<?php
class Engine_Filter_FileUniqueName implements Zend_Filter_Interface
{
	private $_targetDir;
	private $_patternName;
	private $_randomName = false;

	/**
     * Constructor for filter
     *
     * @param array $options Options to set
     */
	public function __construct($options)
	{
       if ($options instanceof Zend_Config) {
            $options = $options->toArray();
        } elseif (is_string($options)) {
            $options = array('targetDir' => $options);
        } elseif (!is_array($options)) {
            throw new Zend_Filter_Exception('Invalid options argument provided to filter');
        }

        if (isset($options['targetDir']) && $options['targetDir']) {
            $this->_targetDir = $options['targetDir'];
        } else {
            throw new Zend_Filter_Exception('Invalid target directory argument provided to filter');
        }

        if (isset($options['patternName']) && $options['patternName']) {
            $this->_patternName = $options['patternName'];
        }

        if (isset($options['randomName'])) {
            $this->_randomName = $options['patternName'];
        }
	}


	/**
     * Translit input string
     *
     * @param string $value filename
     * @return string
     */
	private function translitString($value)
	{
		$fromSymbol = array(" ", "'", '"', "`", '/', '\\', ':', '*', '?', '<', '>', '|');
		$toSymbol = array("_",  "",  "",  "",  "",  "",  "",  "",  "",  "",  "",  "",);
		$value = str_replace($fromSymbol, $toSymbol, $value);
		
		$cyr  = array(
			'а','б','в','г','д','е','ж', 'з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ц', 'ч', 'ш', 'щ',  'ъ','ы','ь','ю', 'я',
			'А','Б','В','Г','Д','Е','Ж', 'З','И','Й','К','Л','М','Н','О','П','Р','С','Т','У','Ф','Х','Ц', 'Ч', 'Ш', 'Щ',  'Ъ','Ы','Ь','Ю', 'Я'
		);
		$lat = array(
			'a','b','v','g','d','e','zh','z','i','y','k','l','m','n','o','p','r','s','t','u','f','h','ts','ch','sh','sch','', 'y','', 'yu','ya',
			'a','b','v','g','d','e','zh','z','i','y','k','l','m','n','o','p','r','s','t','u','f','h','ts','ch','sh','sch','', 'y','', 'yu','ya'
		);

		$value = str_replace($cyr, $lat, $value);
		
		$value = substr($value, 0, 100);

		return $value;
	}


   /**
     * Finds unique filname and move source file to need directory with unique filename
     *
     * @param string $value full filename
     * @return string
     */


	public function filter($value)
	{
		if (!$value) {
        	return $value;
		}

        $fullFilePath = $value;
        $fileInfo = pathinfo($fullFilePath);
        $fileName = $fileInfo['filename'];
        $filePath = WEB_ROOT . $this->_targetDir . '/';
		$fileExtention = '.' . $fileInfo['extension'];


		if ($this->_randomName) {
			$fileName = md5(mktime().$fileName);
		} else {
			$fileName = (!$this->_patternName) ? $this->translitString($fileName) : $this->translitString($this->_patternName);
		}

	   $tempFileBaseName = $fileName . $fileExtention;

	   if (file_exists($filePath . $tempFileBaseName)) {
   	        $i = 0;

   	        while (file_exists($filePath . $tempFileBaseName)) {
    	        $tempFileBaseName = $fileName . "_" . $i++ . $fileExtention;
        	}
	    }

	    copy($value, $filePath . $tempFileBaseName);

	    return $filePath . $tempFileBaseName;
   }
}